A pointer in C is a variable that stores the memory address of another variable. It allows indirect access to the value of the variable it points to. Pointers are used to manage memory, dynamically allocate memory, and efficiently handle data structures.
To declare a pointer in C, use the * symbol before the pointer variable name, followed by the data type that the pointer points to.
data_type *pointer_variable_name;
#include <stdio.h>
int main() {
int num = 10; // A regular integer variable
int *ptr; // Declare a pointer to an integer
ptr = # // Store the address of 'num' in the pointer
// Print the value of 'num' using the pointer
printf("Value of num: %d\n", *ptr);
// Change the value of 'num' using the pointer
*ptr = 25;
// Print the updated value of 'num'
printf("Updated value of num: %d\n", num);
// Working with arrays using pointers
int arr[5] = {1, 2, 3, 4, 5};
int *arrPtr = arr; // Point to the first element of the array
// Print array elements using pointer arithmetic
printf("Array Elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", *(arrPtr + i));
}
printf("\n");
return 0;
}
Value of num: 10
Updated value of num: 25
Array Elements: 1 2 3 4 5
(ptr =
& num;)
. Now, ptr points to the memory location of num.What is a pointer in C?
What does a pointer store in C?
What is the symbol for the address-of operator in C?
What is used to access the value a pointer points to in C?
What type of memory does malloc allocate in C?